import { computed } from 'vue'

import { useWs } from '@/hooks'
import { dayjs, fmtDuration } from '@/utils'

export function useInit() {
  const { sendProxy, dataProxy } = useWs()

  const info = computed<any>(() => dataProxy.value.data)

  const startTime = computed(() =>
    dayjs()
      .subtract(info.value.node.uptime, 'seconds')
      .format('YYYY-MM-DD HH:mm:ss')
  )

  function filterSize(size: number) {
    return `${(size / 1024 / 1024 / 1024).toFixed(2)}GB`
  }

  function filterPercent(percent: number) {
    return `${percent}%`
  }

  function filterValue(value: number) {
    if (value < 1) {
      return filterPercent(value)
    }
    return filterSize(value)
  }

  function filterUptime(value: number) {
    return fmtDuration(value, 'seconds', 'D天H小时mm分钟ss秒')
  }

  try {
    sendProxy('serverInfo')
  } catch (error) {
    console.error(error)
  }

  return {
    info,
    startTime,
    filterSize,
    filterPercent,
    filterValue,
    filterUptime
  }
}
